package com.btcode.orm.model.generater;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.fieldtype.FieldType;
import com.btcode.db.result.Row;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.orm.conver.IModelConver;
import java.util.List;

public class OracleGenerater extends ModelGenerater {


    /**
     * @param executer
     */
    public OracleGenerater(IDataExecuter executer) {
        super(executer);
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getTableInfoModels()
     */
    @Override
    public List<TableInfoModel> getTableInfoModels(String tableName) {
        String sql = "select t.COLUMN_NAME COLUMN_NAME,\n" + "       t.DATA_TYPE DATA_TYPE,\n"
            + "       t2.constraint_type\n" + "  from user_tab_cols t\n"
            + "  left join user_cons_columns t1 on t1.column_name = t.column_name\n"
            + "                                and t1.table_name = '" + tableName
            + "' and t1.position is not null\n"
            + "  left join user_constraints t2 on t1.constraint_name = t2.constraint_name and t2.index_name is not null\n"
            + "                               and t2.table_name = '" + tableName + "'\n"
            + " where t.table_name = '" + tableName + "'";

        List<Row> cloumnss = executer.queryList(sql, null);

        for (Row cloumn : cloumnss) {
            String constraintType = cloumn.getSafeString("CONSTRAINT_TYPE");
            if (constraintType.equals("P")) {
                cloumn.put("IS_KEY", "true");
            }
            else if (constraintType.equals("U")) {
                cloumn.put("IS_UNIQUE", "true");
            }
        }

        IModelConver modelConver = new DefaultModelConver();
        List<TableInfoModel> tableInfoModels = modelConver.converToORMModels(cloumnss,
                                                                             TableInfoModel.class);

        return tableInfoModels;
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getFieldJavaType(java.lang.String)
     */
    @Override
    public String getFieldJavaType(String type) {
        type = type.toUpperCase();

        if ("VARCHAR2".equals(type) || "character".equals(type)) {
            return "String";
        }
        else if ("DATE".equals(type)) {
            return "Date";
        }
        else if (type.indexOf("TIMESTAMP") > -1) {
            return "Date";
        }
        else if ("NUMERIC".equals(type)) {
            return "Double";
        }
        else if ("NUMBER".equals(type)) {
            return "Double";
        }
        else {
            return "String";
        }
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getFieldEmunType(java.lang.String)
     */
    @Override
    public FieldType getFieldEmunType(String type) {
        type = type.toUpperCase();

        if ("VARCHAR2".equals(type) || "character".equals(type)) {
            return FieldType.String;
        }
        else if ("DATE".equals(type)) {
            return FieldType.DateTime;
        }
        else if (type.indexOf("TIMESTAMP") > -1) {
            return FieldType.DateTime;
        }
        else if ("NUMERIC".equals(type)) {
            return FieldType.Double;
        }
        else {
            return FieldType.String;
        }
    }

}
